package com.fafu.rbac.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.fafu.common.web.bean.PageBean;
import com.fafu.common.web.bean.Result;
import com.fafu.common.web.bean.ResultUtil;
import com.fafu.common.web.controller.BaseController;
import com.fafu.meta.domain.entity.Province;
import com.fafu.meta.domain.query.ProvinceQuery;
import com.fafu.meta.service.ProvinceService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * (tb_province)表控制层
 *
 * @author hc
 */

@RestController
@RequestMapping("/province")
public class ProvinceController extends BaseController<Province> {
    public ProvinceController(IService<Province> service) {
        super(service);
    }

    @Resource
    private ProvinceService provinceService;

    @PostMapping("/list")
    public Result list(@RequestBody ProvinceQuery provinceQuery) {
        IPage<Province> page = new Page<>(provinceQuery.getPageNum(), provinceQuery.getPageSize());

        LambdaQueryWrapper<Province> queryWrapper = new LambdaQueryWrapper<>();
        if (provinceQuery.getArea() != null) {
            queryWrapper.like(Province::getArea, provinceQuery.getArea());
        }

        if (provinceQuery.getName() != null) {
            queryWrapper.like(Province::getName, provinceQuery.getName());
        }
        if (provinceQuery.getState() != null) {
            queryWrapper.eq(Province::getState, provinceQuery.getState());
        }

        provinceService.page(page, queryWrapper);

        PageBean<Province> pageBean = PageBean.init(page);
        return ResultUtil.success(pageBean);
    }

}
